package com.yilian.xlmusic.android.view.adapter;

import android.graphics.Color;
import android.text.SpannableString;
import android.view.View;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.yilian.xlmusic.android.R;
import com.yilian.xlmusic.android.databinding.ItemHintBinding;
import com.yilian.xlmusic.android.utils.HistoryRecord;
import com.yilian.xlmusic.android.utils.Utils;
import com.yilian.xlmusic.android.view.bean.HistoryPresenter;
import com.yilian.xlmusic.android.view.bean.SharedMessage;
import com.yilian.xlmusic.android.view.bean.SharedViewModel;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class HintAdapter extends BaseQuickAdapter<String, BaseDataBindingHolder<ItemHintBinding>> {
    private String value;
    SharedViewModel model;
    private HistoryPresenter historyPresenter = new HistoryPresenter();

    public HintAdapter(int layoutResId, @Nullable List<String> data, String value, SharedViewModel sharedViewModel) {
        super(layoutResId, data);
        model = sharedViewModel;
        this.value = value;
    }
    @Override
    protected void convert(@NotNull BaseDataBindingHolder<ItemHintBinding> holder, String s) {
        SpannableString spannableString = Utils.matcherSearchTitle(Color.parseColor("#504AEF"),s,value);
        ItemHintBinding binding = holder.getDataBinding();
        if(null!=binding){
            binding.itemHintText.setText(spannableString);
            binding.itemHintDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SharedMessage message = new SharedMessage();
                    message.setType(0);
                    message.setValue(s);
                    model.select(message);
                }
            });
            setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                    SharedMessage message = new SharedMessage();
                    message.setType(3);
                    message.setValue(s);
                    model.select(message);
                    historyPresenter.goSearch(getContext(),message.getValue(),model,getRecyclerView());
                    //写入历史记录
                    HistoryRecord.saveSearchHistory(getContext(), message.getValue());
                }
            });
        }
    }
}
